package com.dong.mcd.control;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dong.mcd.model.Dict;
import com.dong.mcd.param.DictParam;
import com.dong.mcd.pub.BaseAction;
import com.dong.mcd.service.IDictService;
import com.dong.mcd.util.JSONUtil;

@Controller
@RequestMapping("/system/dict")
public class DictAction  extends BaseAction {

	@Autowired
	private IDictService dictService;
	
	/**
	 * 查询菜单
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/query")
	public void queryDict(HttpServletRequest request, HttpServletResponse response,DictParam param) throws Exception {
		List<Dict> menuList= dictService.query(param);
		writeJson(response, menuList);
	}
	
	@RequestMapping("/page")
	public void queryPage(HttpServletRequest request, HttpServletResponse response,DictParam param) throws Exception {
		int pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		writeJson(response, dictService.queryPage(param, pageIndex, pageSize).getPageMap());
	}
	
	@RequestMapping("/modify")
	public void modifyDict(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String json = request.getParameter("jsonData");
		try {
			Dict dict = JSONUtil.toObject(json, Dict.class);
			if (StringUtils.isEmpty(dict.getCode())){
				writeError(response, "字典编号不能为空");
				return;
			}
			if (StringUtils.isEmpty(dict.getName())){
				writeError(response, "字典名称不能为空");
				return;
			}
			if (StringUtils.isEmpty(dict.getType())){
				writeError(response, "字典类型不能为空");
				return;
			}
			dictService.modifyDict(dict);
			writeSuccess(response, "修改成功");
		} catch (Exception e) {
			writeError(response, "其他错误");
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/add")
	public void addDict(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String json = request.getParameter("jsonData");
		try {
			Dict dict = JSONUtil.toObject(json, Dict.class);
			if (StringUtils.isEmpty(dict.getCode())){
				writeError(response, "字典编号不能为空");
				return;
			}
			if (StringUtils.isEmpty(dict.getName())){
				writeError(response, "字典名称不能为空");
				return;
			}
			if (StringUtils.isEmpty(dict.getType())){
				writeError(response, "字典类型不能为空");
				return;
			}
			if (dictService.getDictById(dict.getCode()) != null){
				writeError(response, "字典编号已存在");
				return;
			}
			dictService.addDict(dict);
			writeSuccess(response, "添加成功");
		} catch (Exception e) {
			writeError(response, "其他错误");
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/delete")
	public void deleteDict(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String id = request.getParameter("code");
		try {
			if (StringUtils.isEmpty(id)){
				writeError(response, "字典编号不能为空");
				return;
			}
			dictService.removeDict(id);
			writeSuccess(response, "删除成功");
		} catch (Exception e) {
			writeError(response, "其他错误");
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/getDict")
	public void getDict(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String dictId = request.getParameter("dictId");
		writeJson(response, dictService.getDictById(dictId));
	}
	
}
